package lk.icta.common.entity;

import lk.icta.common.AppConstants;

import javax.persistence.*;
import java.io.Serializable;

/**
 * @author Mahesha Kalpanie, Supun Nimesh Karunathilaka
 */
@Entity
@Table(schema = "COMMON_RAIL")
@NamedQueries({
        @NamedQuery(name = "findAllDSDivisions", query = "SELECT d FROM DSDivision d"),
        @NamedQuery(name = "get.all.divisions.by.districtId", query = "SELECT d FROM DSDivision d WHERE d.district.id = :districtUKey"),
        @NamedQuery(name = "get.dsDivision.by.code", query = "SELECT d FROM DSDivision d" +
                " WHERE d.code = :dsDivisionId AND d.district=:district"),
        @NamedQuery(name = "get.dsDivision.by.anyName", query = "SELECT d FROM DSDivision d " +
                "WHERE d.siDivisionName = :siName OR d.enDivisionName = :enName OR d.taDivisionName = :taName")
})
public class  DSDivision implements Serializable {
    /**
     * A system generated unique key
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    /**
     * This is the standard D.S. Division code as per the location code database
     */
    @Column(updatable = false, nullable = false)
    private int code;

    /**
     * The relationship to District of which this D.S Division belongs
     * todo: remove this name
     */
    @ManyToOne
    @JoinColumn(nullable = false, updatable = false)
    private District district;

    @Column(nullable = false, updatable = false)
    private String siDivisionName;
    @Column(nullable = false, updatable = false)
    private String enDivisionName;
    @Column(nullable = false, updatable = false)
    private String taDivisionName;

    @Column(columnDefinition = "smallint not null default 1")
    private boolean active;

    /*
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(schema = "COMMON_RAIL", name = "DSDIVISION_LOCALAUTHORITY")
    private Set<LocalAuthorityType> localAuthority;    */

    public DSDivision() {
    }

    public DSDivision(District district, int dsDivisionId,
                      String siDivisionName, String enDivisionName, String taDivisionName, boolean active) {
        this.district = district;
        this.id = dsDivisionId;
        this.siDivisionName = siDivisionName;
        this.enDivisionName = enDivisionName;
        this.taDivisionName = taDivisionName;
        this.active = active;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public District getDistrict() {
        return district;
    }

    public void setDistrict(District district) {
        this.district = district;
    }

    public String getSiDivisionName() {
        return siDivisionName;
    }

    public void setSiDivisionName(String siDivisionName) {
        this.siDivisionName = siDivisionName;
    }

    public String getEnDivisionName() {
        return enDivisionName;
    }

    public void setEnDivisionName(String enDivisionName) {
        this.enDivisionName = enDivisionName;
    }

    public String getTaDivisionName() {
        return taDivisionName;
    }

    public void setTaDivisionName(String taDivisionName) {
        this.taDivisionName = taDivisionName;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public int getDistrictId() {
        return district.getId();
    }

    public void setDistrictId(int districtId) {
        this.district.setId(districtId);
    }

    /*
    public Set<LocalAuthorityType> getLocalAuthorityType() {
        return localAuthority;
    }

    public void setLocalAuthorityType(Set<LocalAuthorityType> localAuthority) {
        this.localAuthority = localAuthority;
    }  */

    public String getDSDivisionNameByLanguage(String language) {
        String divisionName = "";
        if (AppConstants.ENGLISH.equals(language)) {
            divisionName = getEnDivisionName();
        } else if (AppConstants.SINHALA.equals(language)) {
            divisionName = getSiDivisionName();
        } else if (AppConstants.TAMIL.equals(language)) {
            divisionName = getTaDivisionName();
        }

        return divisionName;
    }
}
